package main

import (
	"cabinet/bootload"
	"cabinet/pkg/global"
	"cabinet/router"
	"cabinet/util"
	"log"

	"fmt"

	"github.com/gin-gonic/gin"
)

func main() {
	// 从配置文件读取配置
	bootload.Init()

	//运行模式
	if !global.Conf.System.Debug {
		gin.SetMode(gin.ReleaseMode)
	}
	// 装载路由
	r := router.NewRouter()

	// 初始化MQTT客户端
	mqttClient, err := util.NewMQTTClient("tcp://broker.emqx.io:1883", "cabinet-server")
	if err != nil {
		log.Fatal("Failed to connect to MQTT broker:", err)
	}
	defer mqttClient.Close()
	//柜体寄存
	r = router.SetupRouter(bootload.DB, mqttClient)
	err = r.Run(fmt.Sprintf(":%d", global.Conf.System.Port))
	if err != nil {
		panic(err) // 启动失败，退出程序
	}
}
